package com.zj.sso.example.controller;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zy.sso.client.dto.login.ClientInfo;
import com.zy.sso.client.dto.login.LoginDTO;
import com.zy.sso.client.dto.login.inter.ILogin;
import com.zy.sso.client.utils.WebUtil;

@RestController
public class UserController {

	@PreAuthorize("hasRole('ADMIN')")
	@RequestMapping("/user/{id}")
	public String getUser(@PathVariable("id") String id) {
		System.out.println("getUser_ADMIN");
		return id;

	}

	@PreAuthorize("hasRole('INTERNAL_USER')")
	@RequestMapping("/api/in/{id}")
	public String getUser3(@PathVariable("id") String id) {
		System.out.println("getUser_USER");
		return id;
	}
	
	@PreAuthorize("hasRole('INTERNAL_USER')")
	@RequestMapping("/api/test/{id}")
	public LoginDTO getUser5(@Validated({ILogin.class}) @RequestBody LoginDTO loginDTO) {
		System.out.println("getUser_USER");
		System.out.println(loginDTO.getAccount());
		return loginDTO;
	}
	
	@RequestMapping("/test/test2/{id}")
	public LoginDTO getUser6( @Validated({ILogin.class}) @RequestBody LoginDTO loginDTO) {
		System.out.println("getUser_USER");
		System.out.println(loginDTO.getAccount());
		return loginDTO;
	}
	
	@PreAuthorize("hasRole('OUT_USER')")
	@RequestMapping("/api/out/{id}")
	public String getUser4(@PathVariable("id") String id) {
		System.out.println("getUser_USER");
		return id;
	}

	public static void main(String[] args) throws IllegalAccessException, UnsupportedEncodingException, JsonProcessingException {
		//get 请求无参数
		//get0();
		//get 请求带参数
		//get1();
		//body传参(目前只支持json) ->body为空
		//bodyJson0();
		//list 参数
		//bodyJson1();
		//map 参数
		//bodyJson2();
		//object 参数
		bodyJson3();
		
	}
	
	static void get0() throws IllegalAccessException, UnsupportedEncodingException {
		String sys = "app";
		String key = "abc123";
		String sign = WebUtil.genQueryStr(null, key, sys);
		System.out.println(sign);
	}
	
	static void get1() throws IllegalAccessException, UnsupportedEncodingException {
		String sys = "app";
		String key = "abc123";
		Map<String,Object> map = new HashMap<>();
		map.put("name", "test");
		map.put("age", 20);
		map.put("birthday", LocalDateTime.now());
		System.out.println(map);
		String sign = WebUtil.genQueryStr(map, key, sys);
		System.out.println(sign);
	}
	
	static void bodyJson0() throws IllegalAccessException, UnsupportedEncodingException {
		String sys = "app";
		String key = "abc123";
		String sign = WebUtil.genQueryStr(null, key, sys);
		System.out.println(sign);
	}
	
	static void bodyJson1() throws IllegalAccessException, UnsupportedEncodingException, JsonProcessingException {
		String sys = "app";
		String key = "abc123";
		List<String> list = new ArrayList<>();
		list.add("ttt");
		list.add("dddd");
		System.out.println(new ObjectMapper().writeValueAsString(list));
		String sign = WebUtil.genQueryStr(list, key, sys);
		System.out.println(sign);
	}
	
	static void bodyJson2() throws IllegalAccessException, UnsupportedEncodingException, JsonProcessingException {
		String sys = "app";
		String key = "abc123";
		Map<String,Object> map = new HashMap<>();
		map.put("name", "test");
		map.put("age", 20);
		map.put("birthday", LocalDateTime.now());
		System.out.println(new ObjectMapper().writeValueAsString(map));
		String sign = WebUtil.genQueryStr(map, key, sys);
		System.out.println(sign);
	}

	static void bodyJson3() throws JsonProcessingException, IllegalAccessException, UnsupportedEncodingException {
		String sys = "app";
		String key = "abc123";
		LoginDTO loginDTO = new LoginDTO();
		loginDTO.setAccount("account");
		loginDTO.setPassword("password");
		loginDTO.setFromSys("from_sys");
		ClientInfo clientInfo = new ClientInfo();
		clientInfo.setAddress("address");
		clientInfo.setImei("imei");
		loginDTO.setClientInfo(clientInfo);
		System.out.println(new ObjectMapper().writeValueAsString(loginDTO));
		String sign = WebUtil.genQueryStr(loginDTO, key, sys);
		System.out.println(sign);
	}
}
